<?php
namespace Hr\OnlineBundle\Twig\Extensions;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
// use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
// http://stackoverflow.com/questions/11102344/symfony2-use-translator-in-a-twig-filter-extension

class HrOnlineExtension extends \Twig_Extension
{
	private $translator;

	private $generator;

    protected $container;

    protected $request;


	public function setTranslator(Translator $translator)
    {
        $this->translator = $translator;
    }

	public function __construct(Translator $translator, $container, UrlGeneratorInterface $generator)
    {
         $this->translator = $translator;

		 $this->container = $container;

		 $this->generator = $generator;

          if ($this->container->isScopeActive('request')) {
                $this->request = $this->container->get('request');
          }

    }

    public function getFilters()
    {
        return array(
			'lang_path' => new \Twig_Filter_Method($this, 'langPathFilter'),
            'time_ago' => new \Twig_Filter_Method($this, 'timeAgoFilter'),
			'price' => new \Twig_Filter_Method($this, 'priceFilter'),
        );
    }


   public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
    {
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
        $price = '$'.$price;
        return $price;
    }


    public function langPathFilter($locale)
    {
		$currentRoute = $this->request->get('_route');
		
		$url = "";
		
		$params = $this->request->get('_route_params');
		unset($params['_locale'] );
		
		$languages = $this->container->getParameter('hr_online.languages', array('en','fr','de'));
		foreach ($languages as &$value){
			$value = '/'.$value;
		}

		if ($currentRoute=='hr_online_show_position' || $currentRoute=='hr_online_show_position_switch') {
			
			 $url.= $this->generator->generate($currentRoute, $params);
			
			 $url = str_replace($languages,'',$url);

			 $url.= '/'.$locale ;

		} else {
			 $url.= $this->generator->generate($currentRoute, array('_locale' => $locale));
		}

        return $url;
    }


    public function timeAgoFilter(\DateTime $dateTime = null)
    {
		if ($dateTime == null) {
			return "";
		}

		$second = $this->translator->trans('second');
		$minute = $this->translator->trans('minute');
		$hour = $this->translator->trans('hour');
		$day = $this->translator->trans('day');
		$seconds = $this->translator->trans('seconds');
		$minutes = $this->translator->trans('minutes');
		$hours = $this->translator->trans('hours');
		$days = $this->translator->trans('days');
	

        $delta = time() - $dateTime->getTimestamp();
        if ($delta < 0)
            throw new \InvalidArgumentException("createdAgo is unable to handle dates in the future");

        $duration = "";
        if ($delta < 60)
        {
            // Seconds
            $time = $delta;
            $duration = $time . " " . (($time === 0 || $time > 1) ? $seconds : $second);
        }
        else if ($delta < 3600)
        {
            // Mins
            $time = floor($delta / 60);
            $duration = $time . " " . (($time > 1) ? $minutes : $minute);
        }
        else if ($delta < 86400)
        {
            // Hours
            $time = floor($delta / 3600);
            $duration = $time . " " . (($time > 1) ? $hours : $hour);
        }
        else
        {
            // Days
            $time = floor($delta / 86400);
            $duration = $time . " " . (($time > 1) ? $days : $day);
        }

		$timeAgo = 	$this->translator->trans('%duration% ago', array('%duration%' => $duration));

        return $timeAgo;
    }

    public function getName()
    {
        return 'hr_online_extension';
    }
}